﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace HYFloorPipeModule
{
	public partial class LineStyleEditForm : System.Windows.Forms.Form
	{
		public LineStyleEditForm()
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
		}

		public LineStyleEditForm(GraphicsStyle gsIn, GraphicsStyle gsOut, List<GraphicsStyle> styles, bool bSingleLine = false)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.Icon = Resource.YArchitechIcon;
			this.Text = "管线样式管理";
			this.lineStyle = styles;
			this.graphicStyleIn = gsIn;
			this.graphicStyleOut = gsOut;
			this.InitForm();
			this.chbSameIn.Checked = (bSingleLine || gsIn.Name == gsOut.Name);
		}

		public LineStyleEditForm(string lineTypeName, GraphicsStyle gs)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.Icon = Resource.YArchitechIcon;
			this.Text = this.Text + "-" + lineTypeName;
		}

		private void InitForm()
		{
			this.LineColor.Text = "█RGB 000 255 064";
            this.LineColor.ForeColor = System.Drawing.Color.FromArgb(0, 255, 64);
			this.LineColorOut.Text = "█RGB 000 255 064";
            this.LineColorOut.ForeColor = System.Drawing.Color.FromArgb(0, 255, 64);
			this.InitCmbContant();
			this.bInit = true;
			this.bOutInit = true;
			if (this.graphicStyleIn == null)
			{
				GraphicsStyle graphicsStyle = this.lineStyle.FirstOrDefault((GraphicsStyle style) => style.Name == "地热盘管供水");
				if (graphicsStyle == null)
				{
					this.cmbInType.SelectedIndex = 0;
				}
				else
				{
					this.cmbInType.SelectedItem = graphicsStyle.Name;
				}
			}
			else
			{
				this.cmbInType.SelectedItem = this.graphicStyleIn.Name;
			}
			if (this.graphicStyleOut == null)
			{
				GraphicsStyle graphicsStyle2 = this.lineStyle.FirstOrDefault((GraphicsStyle style) => style.Name == "地热盘管回水");
				if (graphicsStyle2 == null)
				{
					this.cmbOutType.SelectedIndex = 1;
				}
				else
				{
					this.cmbOutType.SelectedItem = graphicsStyle2.Name;
				}
			}
			else
			{
				this.cmbOutType.SelectedItem = this.graphicStyleOut.Name;
			}
			this.bInit = true;
			this.bOutInit = true;
			this.GetGsLineColorAndWidth();
		}

		private void GetGsLineColorAndWidth()
		{
			if (this.graphicStyleIn == null)
			{
				return;
			}
			Category graphicsStyleCategory = this.graphicStyleIn.GraphicsStyleCategory;
            Autodesk.Revit.DB.Color lineColor = graphicsStyleCategory.LineColor;
			if (lineColor != null)
			{
				this._cLineColor = this.ColorsChange(lineColor, false);
			}
            int? lineWeight = graphicsStyleCategory.GetLineWeight((GraphicsStyleType)1);
			this.cmbLineWidth.SelectedIndex = ((lineWeight != null) ? lineWeight.Value : 0);
			if (this.graphicStyleOut == null)
			{
				return;
			}
			Category graphicsStyleCategory2 = this.graphicStyleOut.GraphicsStyleCategory;
			lineColor = graphicsStyleCategory2.LineColor;
			if (lineColor != null)
			{
				this._cLineColorOut = this.ColorsChange(lineColor, true);
			}
            lineWeight = graphicsStyleCategory2.GetLineWeight((GraphicsStyleType)1);
			this.cmbLineWidthOut.SelectedIndex = ((lineWeight != null) ? lineWeight.Value : 0);
		}

		private void InitCmbContant()
		{
			this.cmbLineWidth.Items.Add("默认");
			for (int i = 1; i < 17; i++)
			{
				this.cmbLineWidth.Items.Add(i);
			}
			this.cmbLineWidth.SelectedIndex = 0;
			this.cmbLineWidthOut.Items.Add("默认");
			for (int j = 1; j < 17; j++)
			{
				this.cmbLineWidthOut.Items.Add(j);
			}
			this.cmbLineWidthOut.SelectedIndex = 0;
			this.cmbInType.DataSource = this.GetTypeDataSource(this.graphicStyleOut);
			this.cmbOutType.DataSource = this.GetTypeDataSource(this.graphicStyleIn);
		}

        private System.Drawing.Color ColorsChange(Autodesk.Revit.DB.Color rvColor, bool bOut = false)
		{
			string newRGB = this.getNewRGB(rvColor.Red);
			string newRGB2 = this.getNewRGB(rvColor.Green);
			string newRGB3 = this.getNewRGB(rvColor.Blue);
			string text = string.Concat(new string[]
			{
				"█RGB ",
				newRGB,
				" ",
				newRGB2,
				" ",
				newRGB3
			});
			if (!bOut)
			{
                this.LineColor.ForeColor = System.Drawing.Color.FromArgb((int)rvColor.Red, (int)rvColor.Green, (int)rvColor.Blue);
				this.LineColor.Text = text;
			}
			else
			{
                this.LineColorOut.ForeColor = System.Drawing.Color.FromArgb((int)rvColor.Red, (int)rvColor.Green, (int)rvColor.Blue);
				this.LineColorOut.Text = text;
			}
            return System.Drawing.Color.FromArgb((int)rvColor.Red, (int)rvColor.Green, (int)rvColor.Blue);
		}

        public System.Drawing.Color GetXmlDataColor(string rgb)
		{
            System.Drawing.Color result = default(System.Drawing.Color);
			if (rgb != "默认")
			{
                result = System.Drawing.Color.FromArgb(int.Parse(rgb.Split(new char[]
				{
					' '
				}).ElementAt(1)), int.Parse(rgb.Split(new char[]
				{
					' '
				}).ElementAt(2)), int.Parse(rgb.Split(new char[]
				{
					' '
				}).ElementAt(3)));
			}
			else
			{
                result = System.Drawing.Color.FromArgb(128, 128, 128);
			}
			return result;
		}

        public int GetIntAboutColor(System.Drawing.Color color)
		{
			return (int)color.B << 16 | (int)((ushort)((int)color.G << 8 | (int)color.R));
		}

		public string getNewRGB(byte R)
		{
			string result = "";
			if (R <= 9)
			{
				result = "00" + R;
			}
			else if (R <= 99 && R >= 10)
			{
				result = "0" + R;
			}
			if (R >= 100)
			{
				result = R.ToString();
			}
			return result;
		}

        private Autodesk.Revit.DB.Color GetLineColor(System.Drawing.Color color)
		{
            return new Autodesk.Revit.DB.Color(color.R, color.G, color.B);
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			this.graphicStyleIn.GraphicsStyleCategory.LineColor = this.GetLineColor(this._cLineColor);
			this.graphicStyleIn.GraphicsStyleCategory.SetLineWeight(this.nLineWidth, (Autodesk.Revit.DB.GraphicsStyleType)1);
			this.graphicStyleOut.GraphicsStyleCategory.LineColor = this.GetLineColor(this._cLineColorOut);
			this.graphicStyleOut.GraphicsStyleCategory.SetLineWeight(this.nLineWidthOut, (Autodesk.Revit.DB.GraphicsStyleType)1);
			base.DialogResult = DialogResult.OK;
			base.Close();
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void LineColor_Click(object sender, EventArgs e)
		{
			ColorDialog colorDialog = new ColorDialog();
			colorDialog.AllowFullOpen = true;
			colorDialog.FullOpen = true;
            System.Drawing.Color xmlDataColor = this.GetXmlDataColor(this.LineColor.Text);
			if (!xmlDataColor.IsKnownColor)
			{
				int[] array = new int[16];
				array[0] = this.GetIntAboutColor(xmlDataColor);
				colorDialog.CustomColors = array;
			}
			colorDialog.Color = xmlDataColor;
			DialogResult dialogResult = colorDialog.ShowDialog();
			if (DialogResult.OK == dialogResult)
			{
				string newRGB = this.getNewRGB(colorDialog.Color.R);
				string newRGB2 = this.getNewRGB(colorDialog.Color.G);
				string newRGB3 = this.getNewRGB(colorDialog.Color.B);
				string text = string.Concat(new string[]
				{
					"█RGB ",
					newRGB,
					" ",
					newRGB2,
					" ",
					newRGB3
				});
				this.LineColor.ForeColor = colorDialog.Color;
				this.LineColor.Text = text;
				this._cLineColor = colorDialog.Color;
				if (this.chbSameIn.Checked)
				{
					this.LineColorOut.ForeColor = colorDialog.Color;
					this.LineColorOut.Text = text;
					this._cLineColorOut = colorDialog.Color;
				}
			}
			this.cmbLineWidth.Focus();
		}

		private void cmbLineWidth_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.nLineWidth = (int)((this.cmbLineWidth.Text == "默认") ? 5 : short.Parse(this.cmbLineWidth.Text));
			if (this.chbSameIn.Checked)
			{
				this.cmbLineWidthOut.SelectedItem = this.cmbLineWidth.SelectedItem;
			}
		}

		private void LineColorOut_Click(object sender, EventArgs e)
		{
			ColorDialog colorDialog = new ColorDialog();
			colorDialog.AllowFullOpen = true;
			colorDialog.FullOpen = true;
            System.Drawing.Color xmlDataColor = this.GetXmlDataColor(this.LineColorOut.Text);
			if (!xmlDataColor.IsKnownColor)
			{
				int[] array = new int[16];
				array[0] = this.GetIntAboutColor(xmlDataColor);
				colorDialog.CustomColors = array;
			}
			colorDialog.Color = xmlDataColor;
			DialogResult dialogResult = colorDialog.ShowDialog();
			if (DialogResult.OK == dialogResult)
			{
				string newRGB = this.getNewRGB(colorDialog.Color.R);
				string newRGB2 = this.getNewRGB(colorDialog.Color.G);
				string newRGB3 = this.getNewRGB(colorDialog.Color.B);
				string text = string.Concat(new string[]
				{
					"█RGB ",
					newRGB,
					" ",
					newRGB2,
					" ",
					newRGB3
				});
				this.LineColorOut.ForeColor = colorDialog.Color;
				this.LineColorOut.Text = text;
				this._cLineColorOut = colorDialog.Color;
			}
			this.cmbLineWidthOut.Focus();
		}

		private void cmbLineWidthOut_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.nLineWidthOut = (int)((this.cmbLineWidthOut.Text == "默认") ? 5 : short.Parse(this.cmbLineWidthOut.Text));
		}

		private void chbSameIn_CheckedChanged(object sender, EventArgs e)
		{
			if (this.chbSameIn.Checked)
			{
				this.gbOut.Enabled = false;
				this.bInit = false;
				this.cmbInType.DataSource = this.GetTypeDataSource(null);
				this.cmbInType.SelectedItem = this.graphicStyleIn.Name;
				this.bInit = true;
				this.cmbOutType.DataSource = this.GetTypeDataSource(null);
				this.cmbOutType.SelectedItem = this.cmbInType.SelectedItem;
				this.LineColorOut.Text = this.LineColor.Text;
				this.cmbLineWidthOut.SelectedItem = this.cmbLineWidth.SelectedItem;
				return;
			}
			this.gbOut.Enabled = true;
			this.cmbOutType.DataSource = this.GetTypeDataSource(this.GetLineStyleByNames(this.cmbInType.Text.Trim()));
			this.cmbOutType.SelectedIndex = 0;
		}

		private void cmbInType_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (!this.bInit)
			{
				return;
			}
			this.graphicStyleIn = this.GetLineStyleByNames(this.cmbInType.Text);
			Category graphicsStyleCategory = this.graphicStyleIn.GraphicsStyleCategory;
            Autodesk.Revit.DB.Color lineColor = graphicsStyleCategory.LineColor;
			if (lineColor != null)
			{
				this._cLineColor = this.ColorsChange(lineColor, false);
			}
            int? lineWeight = graphicsStyleCategory.GetLineWeight((GraphicsStyleType)1);
			this.cmbLineWidth.SelectedIndex = ((lineWeight != null) ? lineWeight.Value : 0);
			if (this.chbSameIn.Checked)
			{
				this.cmbOutType.SelectedItem = this.cmbInType.SelectedItem;
				return;
			}
			this.bOutInit = false;
			this.cmbOutType.DataSource = this.GetTypeDataSource(this.graphicStyleIn);
			this.cmbOutType.SelectedItem = this.graphicStyleOut.Name;
			this.bOutInit = true;
		}

		private void cmbOutType_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (!this.bOutInit)
			{
				return;
			}
			this.graphicStyleOut = this.GetLineStyleByNames(this.cmbOutType.Text);
			Category graphicsStyleCategory = this.graphicStyleOut.GraphicsStyleCategory;
            Autodesk.Revit.DB.Color lineColor = graphicsStyleCategory.LineColor;
			if (lineColor != null)
			{
				this._cLineColorOut = this.ColorsChange(lineColor, true);
			}
            int? lineWeight = graphicsStyleCategory.GetLineWeight((GraphicsStyleType)1);
			this.cmbLineWidthOut.SelectedIndex = ((lineWeight != null) ? lineWeight.Value : 0);
			if (!this.chbSameIn.Checked)
			{
				this.bInit = false;
				this.cmbInType.DataSource = this.GetTypeDataSource(this.graphicStyleOut);
				this.cmbInType.SelectedItem = this.graphicStyleIn.Name;
				this.bInit = true;
			}
		}

		private List<string> GetTypeDataSource(GraphicsStyle graphics = null)
		{
			List<string> styleNames = new List<string>();
			IEnumerable<GraphicsStyle> source = this.lineStyle;
            //Func<GraphicsStyle, bool> <>9__0;
            //Func<GraphicsStyle, bool> predicate;
            //if ((predicate = <>9__0) == null)
            //{
            //    predicate = (<>9__0 = ((GraphicsStyle gs) => !styleNames.Contains(gs.Name)));
            //}
            //foreach (GraphicsStyle graphicsStyle in source.Where(predicate))
            //{
            //    if (graphics == null || graphicsStyle.Name != graphics.Name)
            //    {
            //        styleNames.Add(graphicsStyle.Name);
            //    }
            //}
			return styleNames;
		}

		private GraphicsStyle GetLineStyleByNames(string name)
		{
			return this.lineStyle.FirstOrDefault((GraphicsStyle gs) => name == gs.Name);
		}

		private int nLineWidth = 1;

		private int nLineWidthOut = 1;

        private System.Drawing.Color _cLineColor = System.Drawing.Color.FromArgb(128, 128, 128);

        private System.Drawing.Color _cLineColorOut = System.Drawing.Color.FromArgb(128, 128, 128);

		private readonly List<GraphicsStyle> lineStyle;

		public GraphicsStyle graphicStyleIn;

		public GraphicsStyle graphicStyleOut;

		private bool bInit;

		private bool bOutInit;
	}
}
